package baidu

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"gitee.com/dreamwood/ez-go/tools"
	"io"
	"net/http"
)

func getToken() {
	url := "https://aip.baidubce.com/oauth/2.0/token"
	apiKey := "8DjxHimzW4wNSnLWa9fmfghK"
	secretKey := "Z1xcyOzXrqkY5Y3sTYXqNiP03R5Xwenc"

	resp, err := http.Post(
		fmt.Sprintf("%s?grant_type=client_credentials&client_id=%s&client_secret=%s", url, apiKey, secretKey),
		"application/json", nil,
	)
	if err != nil {
		println(err.Error())
		return
	}
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		println(err.Error())
		return
	}
	println(string(data))
	wanted := make(ss.M)
	err = json.Unmarshal(data, &wanted)
	if err != nil {
		println(err.Error())
		return
	}
	if token, ok := wanted["access_token"]; ok {
		println(token.(string))
	}
}

const token = "24.f4706790e3c6f5ca508c324c37fc4dee.2592000.1715953621.282335-35835339"

func ask() {
	url := "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/image2text/fuyu_8b"
	picPath := "123.png"
	//将png图片转为base64编码

	picContent := tools.ReadFile(picPath)
	dst := base64.StdEncoding.EncodeToString(picContent)
	println(string(dst))
	println(string(url))
	body := ss.M{
		"prompt":      "提取图片中的文字",
		"image":       dst,
		"temperature": 0.8,
	}
	content, err := json.Marshal(body)
	bf := bytes.NewBuffer(content)
	if err != nil {
		println(err.Error())
		return
	}
	resp, err := http.Post(
		fmt.Sprintf("%s?access_token=%s", url, token),
		"application/json", bf,
	)
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		println(err.Error())
		return
	}
	println(string(data))
	wanted := make(ss.M)
	err = json.Unmarshal(data, &wanted)
	if err != nil {
		println(err.Error())
		return
	}
	ez.JsonLog(wanted)

}
